/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package services;

import dao.UserJpaController;
import dao.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.Persistence;
import javax.persistence.PersistenceException;
import pojo.Buy;
import pojo.User;

/**
 *
 * @author khalifaabdelaziz
 */
public class ServiceUser {

    public String login(String email, String password) {
        UserJpaController userController = new UserJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        User userdata = userController.findUserByUser(user);
        if (userdata != null) {
            if(userdata.getActive()==1){
            return "" + userdata.getId();
            }else{
                return "Sorry User Not active";
            }
        } else {
            return "Wrong User data, please try again";
        }
    }
    public String edit(User user) {
        UserJpaController userController = new UserJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
        User userdata = userController.findUser(user.getId());
        user.setId(userdata.getId());
        user.setBuyList(userdata.getBuyList());
        user.setCartList(userdata.getCartList());
        try {
            userController.edit(user);
            return "done";
        } catch (NonexistentEntityException ex) {
            return "Wrong User data, please try again";
        } catch (Exception ex) {
           return "Error";
        }
       
    }
    
    
    public List<User> getUsers(){
        UserJpaController userController = new UserJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
        return userController.findUserEntities();
    }
    public User getUserData(Integer id){
         UserJpaController userController = new UserJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
       return userController.findUser(id);
        
    }
    public List<Buy> getUserHistory(Integer id){
      UserJpaController userController = new UserJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
      return userController.findUser(id).getBuyList();
    }

    public String register(User user) {
        UserJpaController userController = new UserJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
        try {
            userController.create(user);
            return "done";
        } catch (PersistenceException ex) {
            return "user already exists";
        }
    }
//
//    public static void main(String[] args) {
//
//        // System.out.println(new ServiceUser().login("khalifa@gmail.com", "1234"));
//         User user = new User();
//         user.setId(4);
//         user.setActive(1);
//         user.setAddress("asdasd");
//         user.setBirthdate(new Date(1991, 8, 16));
//         user.setCredit("5000");
//         user.setEmail("khalifa14@gmail.com");
//         user.setFname("khalifa");
//         user.setLname("abd");
//         user.setLoginDate(new Date(System.currentTimeMillis()));
//         user.setMobile("01006044940");
//         user.setPassword("1234");
//         user.setUsername("khalifa abdelaziz");
//////         System.out.println(new ServiceUser().register(user));
//////         System.out.println(new ServiceUser().getUsers().get(0).getId());
//////         System.out.println(new ServiceUser().getUsers().get(1).getId());
//         System.out.println(new ServiceUser().edit(user));
//    }
}
